﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace TCHSHue.BLL
{
    public class Paging
    {
        public int PSize { get; set; }
        int pTotal = 1;
        public Paging()
        {
            PSize = 10;
        }

        public Paging(int pSize)
        {
            this.PSize = pSize;
        }
        public string CreatePaging(int total, int pageIndex, string path)
        {
            pTotal = total / PSize;
            if (pTotal * PSize < total)
                pTotal++;
            else if (pTotal == 0)
                return "";
            string str = "<span id='spanPaging' class='paging'>";
            for (int i = 1; i <= pTotal; i++)
            {
                if (i == 1)
                {
                    if (pageIndex == 1)
                    {
                        str += "<a class='Active'>««</a><a class='Active'>«</a>";
                        str += "<a class='Active'>1</a>";
                    }
                    else
                    {
                        str += string.Format("<a href='{0}pageIndex=1'>««</a><a href='{0}pageIndex={1}'>«</a>", path, pageIndex - 1);
                        str += string.Format("<a href='{0}pageIndex=1'>1</a>", path);
                    }
                }
                if (i == pTotal)
                {
                    if (pageIndex == pTotal)
                    {
                        if (i != 1) str += "<a class='Active'>" + pageIndex + "</a>";
                        str += "<a class='Active'>»</a><a class='Active'>»»</a>";
                    }
                    else
                    {
                        str += string.Format("<a href='{0}pageIndex={1}'>{1}</a>", path, pTotal);
                        str += string.Format("<a href='{0}pageIndex={1}'>»</a><a href='{0}pageIndex={2}'>»»</a>", path, pageIndex + 1, pTotal);
                    }
                }
                if (1 < i && i < pTotal)
                    str += string.Format("<a href='{0}pageIndex={1}'>{1}</a>", path, i);
            }
            str += "</span>";
            return str;
        }
    }
}